
import 'package:flutter/material.dart';

class AddTab extends StatefulWidget {
  const AddTab({super.key});

  @override
  _AddTabState createState() => _AddTabState();
}

class _AddTabState extends State {
  final List<Tab> tabs = [];
  int tabIndex = 0;

  @override
  void initState() {
    super.initState();
    tabs.add(const Tab(text: 'Tab1'));
    tabs.add(const Tab(text: 'Tab2'));
  }

  void addTab() {
    setState(() {
      tabIndex++;
      tabs.add(Tab(text: 'Tab$tabIndex'));
    });
  }

  @override
  Widget build(BuildContext context) {
    return  DefaultTabController(
        length: tabs.length,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Flutter Tab Demo'),
            bottom: TabBar(
              tabs: tabs,
            ),
          ),
          body: TabBarView(
            children: tabs.map((Tab tab) {
              return Center(child: Text('This is ${tab.text}'));
            }).toList(),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: addTab,
            child: const Icon(Icons.add),
          ),
        ),
      );

  }
}
